﻿using System;
using System.Web;
using log4net;
using MyLib.IO;
using MyLib.Web.Properties;

namespace MyLib.Web.HttpHandlers {
	/// <summary>
	/// 
	/// </summary>
	public class FileHandler : BaseFileHandler {
		/// <summary>
		/// Internal logger.
		/// </summary>
		private ILog m_logger = LogManager.GetLogger(typeof(FileHandler));

		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <param name="fileObject"></param>
		/// <param name="fileContent"></param>
		protected override void WriteResponse(HttpContext context, FileObject fileObject, byte[] fileContent) {
			if (fileObject == null) {
				throw new HttpException(404, string.Format(Main.FileDoesNotExist, context.Request.RawUrl));
			}

			context.WriteDownloadable(fileObject.Name, fileObject.Size,
				(s) => {
					s.Write(fileContent, 0, fileContent.Length);
				});
		}
	} // end of FileHandler.
}
